package models;

import java.util.ArrayList;
import java.util.Arrays;

/**
 * 
 * @author Marietta
 *
 */
public class Card implements Comparable<Card> {

	private String suit;
	private String rank;


	/**
	 * Card constructor
	 * 
	 * @param suit
	 * @param rank
	 * @throws BadugiException
	 */
	public Card(String suit, String rank) {
		this.setSuit(suit);
		this.setRank(rank);
	}
	
	public int compareTo(Card card) {
		String rankCard1 = this.toString().substring(0, 1);
		String rankCard2 = card.toString().substring(0, 1);
		if (Card.getAllowedRanks().indexOf(rankCard1) > Card.getAllowedRanks().indexOf(rankCard2)) {
			return -1;
		} else if (Card.getAllowedRanks().indexOf(rankCard1) < Card.getAllowedRanks().indexOf(rankCard2)) {
			return 1;
		} else {
			return 0;
		}
	}

	/**
	 * @return ArrayList<String>
	 */
	public static ArrayList<String> getAllowedRanks() {
		return new ArrayList<String>(Arrays.asList("A", "2", "3", "4", "5",
				"6", "7", "8", "9", "0", "J", "Q", "K"));
	}

	/**
	 * 
	 * @return ArrayList<String>
	 */
	public static ArrayList<String> getAllowedSuites() {
		return new ArrayList<String>(Arrays.asList("C", "S", "D",
				"H"));
	}

	/**
	 * @return the suit
	 */
	public String getSuit() {
		return suit;
	}

	/**
	 * @param suit
	 *            the suit to set
	 * 
	 * @throws BadugiException
	 */
	public void setSuit(String suit){
		this.suit = suit;
	}

	/**
	 * @return the rank
	 */
	public String getRank() {
		return rank;
	}

	/**
	 * @param rank
	 *            the rank to set
	 * @throws BadugiException
	 */
	public void setRank(String rank) {
		this.rank = rank;
	}

	@Override
	public String toString() {
		return this.getRank() + this.getSuit();
	}
	
	/**
	 * Get file name representing the card
	 * @return String
	 */
	public String getPictureName() {
		return this.getSuit() + this.getRank() + ".png";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((rank == null) ? 0 : rank.hashCode());
		result = prime * result + ((suit == null) ? 0 : suit.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Card other = (Card) obj;
		if (rank == null) {
			if (other.rank != null)
				return false;
		} else if (!rank.equals(other.rank))
			return false;
		if (suit == null) {
			if (other.suit != null)
				return false;
		} else if (!suit.equals(other.suit))
			return false;
		return true;
	}
	
	

}